package com.peanut.youfupay.service.other;

import com.alibaba.fastjson.JSON;
import com.peanut.youfupay.entity.request.CheckBillFile;
import com.peanut.youfupay.entity.request.RequestMessage;
import com.peanut.youfupay.service.BaseService;
import com.peanut.youfupay.util.*;

import java.util.HashMap;
import java.util.Map;

import static com.peanut.youfupay.constant.MerInfoConstant.*;

/**
 * 对账文件下载（很少使用）
 *
 * @author : chenym
 * @create 2019-09-23 4:19 PM
 **/
public class CheckBillFileService extends BaseService {
    public static void main(String[] args) throws Exception {
        RequestMessage rm = new RequestMessage();
        rm.setReqId("CheckBillFileService" + System.currentTimeMillis());
        //FunCode6004：对账文件下载接口
        rm.setFunCode("6004");
        //企业商户编码
        rm.setMerId("89900000222116027420");
        //版本信息
        rm.setVersion("V1.0");
        String json = toJson();
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        //请求数据
        rm.setReqData(reqDataEncrypt);
        //签名
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));
        String reqStr = JsonUtils.toJson(rm);
        System.out.println("请求参数：" + reqStr);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("reqJson", reqStr);
        //下载文件到本地
        String filePath = "/Users/peanut/Documents/CheckBillFileService/test" + System.currentTimeMillis() + ".xls";
        System.out.println("http请求开始");
        String resData = HttpClientHelper.httpClientFile(YOU_FU_URL, map, CHARSET_UTF_8, filePath);
        System.out.println("http请求结束");
        System.out.println("返回结果：" + resData);

    }

    /**
     * 对账文件请求json
     *
     * @return
     */
    private static String toJson() {
        CheckBillFile checkBillFile = new CheckBillFile();
        checkBillFile.setBillDate("2020-11-03");
        return JSON.toJSONString(checkBillFile);
    }
}
